// miniprogram/pages/demo/demo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    arr: ['json', 'js', 'wxml', 'wxss'],
    arrObj: [
      {
        id: 0,
        name: 'wxml'
      },
      {
        id: 1,
        name: 'js'
      },
      {
        id: 2,
        name: 'wxss'
      },
      {
        id: 3,
        name: 'json'
      },
      {
        id: 4,
        name: 'js'
      },
      {
        id: 5,
        name: 'wxss'
      },
      {
        id: 6,
        name: 'json'
      },
      {
        id: 7,
        name: 'json'
      },
      {
        id: 8,
        name: 'js'
      },
      {
        id: 9,
        name: 'wxss'
      },
      {
        id: 10,
        name: 'json'
      },
    ]
  },

  sort() {
    const length = this.data.arr.length
    for (let i = 0; i < length; i++) {
      let x = Math.floor(Math.random() * length)
      let y = Math.floor(Math.random() * length)
      const temp = this.data.arr[x]
      this.data.arr[x] = this.data.arr[y]
      this.data.arr[y] = temp
    }
    this.setData({
      arr: this.data.arr
    })
  },
  sortobj() {
    const length = this.data.arrObj.length
    for (let i = 0; i < length; i++) {
      let x = Math.floor(Math.random() * length)
      let y = Math.floor(Math.random() * length)
      const temp = this.data.arrObj[x]
      this.data.arrObj[x] = this.data.arrObj[y]
      this.data.arrObj[y] = temp
    }
    this.setData({
      arrObj: this.data.arrObj
    })
  },
  onLogin(event) {
    let userinfo = event.detail.userInfo
    // console.log(event)
    wx.cloud.callFunction({
      name: 'user',
      data: {
        info: userinfo,
        $url: 'login'
      }
    }).then(res => {
      console.log(res)
    })
    // if (userinfo) {
    //   this.setData({
    //     modalShow: false
    //   })
    //   this.triggerEvent('loginSuccess', userinfo)
    // } else {
    //   this.triggerEvent('loginFail')
    // }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    wx.cloud.callFunction({
      name: 'login'
      
    }).then(res => {
      this.setData({
        openid: res.result.openid
      })
      console.log(res)
    })
    console.log(this.foo())
    this.getOne()

  },
  async foo() {
    console.log(123)
  },
  getMusicInfo() {
    wx.cloud.callFunction({
      name: 'tcbrouter',
      data: {
        $url: 'music'
      }
    }).then(res => {
      console.log(res)
    })
  },
  getMovieInfo() {
    wx.cloud.callFunction({
      name: 'tcbrouter',
      data: {
        $url: 'movie'
      }
    }).then(res => {
      console.log(res)
    })
  },
  getOne() {
    wx.cloud.callFunction({
      name: 'getOnehome',
      data: {
        $url: 'home'
      }
    }).then(res => {
      console.log('home:::', res)
      let result = JSON.parse(res.result)

      console.log('result:::', result)
    })
  },
  tabchange(event) {
    console.log(event)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})